using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Visio = Microsoft.Office.Interop.Visio;
using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowDataTypes = Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per frmTransition.
	/// </summary>
	public class FormTransition : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cmbResult;
		private System.Windows.Forms.ComboBox cmbOperator;
		private System.Windows.Forms.ComboBox cmbRexec;
		private System.Windows.Forms.TextBox txtTransitionName;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtToActivity;
		private System.Windows.Forms.TextBox txtFromActivity;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		Visio.Shape m_Shape;
		WorkflowProcesses.XMLTransition xmlTransition;

		private const string Activity_MASTER = "Activity";
		private const string Start_MASTER = "Start";
		private const string End_MASTER = "Intermediate";
		private const string Gateway_MASTER = "Gateway";
		private System.Windows.Forms.GroupBox groupBox1;
		private const string COLLAPSED_SUBPROCESS_MASTER = "Collapsed Sub-Process";

		public FormTransition()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();
			this.LoadControl();
			
			xmlTransition = new WorkflowProcesses.XMLTransition();

		}
		public FormTransition(Visio.Shape oSelectedShape):this()
		{
			try
			{
				this.Text="Transition:" + oSelectedShape.Text;
				this.BindObject(oSelectedShape);
			}
			
			catch(Exception err)
			{
				System.Console.WriteLine(err.Message);
				throw(err);
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormTransition));
			this.cmbResult = new System.Windows.Forms.ComboBox();
			this.cmbOperator = new System.Windows.Forms.ComboBox();
			this.cmbRexec = new System.Windows.Forms.ComboBox();
			this.txtTransitionName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.txtToActivity = new System.Windows.Forms.TextBox();
			this.txtFromActivity = new System.Windows.Forms.TextBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.panel2 = new System.Windows.Forms.Panel();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.panel2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmbResult
			// 
			this.cmbResult.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbResult.Location = new System.Drawing.Point(8, 112);
			this.cmbResult.Name = "cmbResult";
			this.cmbResult.Size = new System.Drawing.Size(544, 21);
			this.cmbResult.Sorted = true;
			this.cmbResult.TabIndex = 2;
			// 
			// cmbOperator
			// 
			this.cmbOperator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbOperator.Location = new System.Drawing.Point(24, 160);
			this.cmbOperator.Name = "cmbOperator";
			this.cmbOperator.Size = new System.Drawing.Size(544, 21);
			this.cmbOperator.TabIndex = 1;
			// 
			// cmbRexec
			// 
			this.cmbRexec.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbRexec.Location = new System.Drawing.Point(24, 288);
			this.cmbRexec.Name = "cmbRexec";
			this.cmbRexec.Size = new System.Drawing.Size(168, 21);
			this.cmbRexec.TabIndex = 3;
			// 
			// txtTransitionName
			// 
			this.txtTransitionName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtTransitionName.Location = new System.Drawing.Point(216, 24);
			this.txtTransitionName.Name = "txtTransitionName";
			this.txtTransitionName.Size = new System.Drawing.Size(160, 20);
			this.txtTransitionName.TabIndex = 22;
			this.txtTransitionName.Text = "";
			this.txtTransitionName.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(24, 144);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(208, 13);
			this.label3.TabIndex = 20;
			this.label3.Text = "Operator";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 96);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(60, 14);
			this.label2.TabIndex = 19;
			this.label2.Text = "Result";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(24, 272);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(456, 14);
			this.label1.TabIndex = 18;
			this.label1.Text = "Set \"Activity From\" to Rexecute when navigate this transition";
			// 
			// txtToActivity
			// 
			this.txtToActivity.BackColor = System.Drawing.Color.White;
			this.txtToActivity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtToActivity.Enabled = false;
			this.txtToActivity.Location = new System.Drawing.Point(392, 40);
			this.txtToActivity.Name = "txtToActivity";
			this.txtToActivity.Size = new System.Drawing.Size(176, 20);
			this.txtToActivity.TabIndex = 17;
			this.txtToActivity.Text = "";
			this.txtToActivity.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// txtFromActivity
			// 
			this.txtFromActivity.BackColor = System.Drawing.Color.White;
			this.txtFromActivity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtFromActivity.Enabled = false;
			this.txtFromActivity.Location = new System.Drawing.Point(24, 40);
			this.txtFromActivity.Name = "txtFromActivity";
			this.txtFromActivity.Size = new System.Drawing.Size(176, 20);
			this.txtFromActivity.TabIndex = 16;
			this.txtFromActivity.Text = "";
			this.txtFromActivity.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.Color.White;
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(16, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(560, 82);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 15;
			this.pictureBox1.TabStop = false;
			// 
			// panel2
			// 
			this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panel2.Controls.Add(this.btnCancel);
			this.panel2.Controls.Add(this.btnSave);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 318);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(584, 56);
			this.panel2.TabIndex = 27;
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(448, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(120, 35);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(16, 8);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(120, 35);
			this.btnSave.TabIndex = 4;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cmbResult);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Location = new System.Drawing.Point(16, 104);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(560, 152);
			this.groupBox1.TabIndex = 28;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Condition";
			// 
			// FormTransition
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(584, 374);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.cmbOperator);
			this.Controls.Add(this.cmbRexec);
			this.Controls.Add(this.txtTransitionName);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtToActivity);
			this.Controls.Add(this.txtFromActivity);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.groupBox1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FormTransition";
			this.ShowInTaskbar = false;
			this.Text = "Transition";
			this.panel2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if ( this.txtTransitionName.Text == "" )
			{
				this.txtTransitionName.Text  = this.cmbOperator.Text;
				
				if ( this.cmbOperator.Text != WorkflowProcesses.XMLTransition.ALWAYS_OPERATOR) 
				{
					this.txtTransitionName.Text+= " " + this.cmbResult.Text;
				}
			}

			m_Shape.Text=this.txtTransitionName.Text;
			xmlTransition.From=this.txtFromActivity.Text;
			xmlTransition.To=this.txtToActivity.Text;
			xmlTransition.Result=this.cmbResult.Text;
			xmlTransition.ReExec=this.cmbRexec.Text;
			xmlTransition.Operator=this.cmbOperator.Text;
			m_Shape.Data1=xmlTransition.GetXML();

			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			xmlTransition = null;
			this.Close();
		}

		public void BindObject(Microsoft.Office.Interop.Visio.Shape oShape)
		{
			m_Shape=oShape;
			if (oShape.Data1!="")
			{
				xmlTransition.Parse(oShape.Data1);
			}

			this.txtTransitionName.Text=m_Shape.Text;
			
			this.cmbRexec.Text=xmlTransition.ReExec;
			this.cmbOperator.Text=xmlTransition.Operator;
			this.txtFromActivity.Text = xmlTransition.From;
			this.txtToActivity.Text = xmlTransition.To;

			Visio.Connects o_Connects=m_Shape.Connects;
			
			foreach (Visio.Connect o_Connect in o_Connects)
			{
				int FlowDirection=o_Connect.FromPart;
				string sShapeId = Utility.GetMasterNameUBase(o_Connect.ToSheet.Master.NameU);
				switch (sShapeId) 
				{
					case Start_MASTER:
					{
						switch (FlowDirection)
						{
							case 9:
								this.txtFromActivity.Text="Start";
								break;
							case 12:
								//TODO: Error
								break;
						}
						break;
					}

					case End_MASTER:
								
						switch (FlowDirection)
						{
							case 9:
								//TODO: Error
								break;
							case 12:
								this.txtToActivity.Text = "End";
								//TODO: Error
								break;
						}
						break;

					case Gateway_MASTER:
					case Activity_MASTER:
					case COLLAPSED_SUBPROCESS_MASTER:
					default:
					{
						Visio.Shape oShape2=o_Connect.ToSheet;

						WorkflowProcesses.XMLActivity activity2Com = new WorkflowProcesses.XMLActivity();
						activity2Com.Parse ( oShape2.Data1);

						switch (FlowDirection)
						{
							case 9:
								this.txtFromActivity.Text=activity2Com.Name;

                                WAPI.DataTypesHelper helper = new WAPI.DataTypesHelper();
                                WorkflowDataTypes.XMLEnumerationValues xmlValues = helper.GetValues(activity2Com.XMLBehaviour.XMLBehaviourExecution.XMLScript.DataType);

                                foreach ( WorkflowDataTypes.XMLDataTypeValue dtValue in xmlValues)
								{
									this.cmbResult.Items.Add (dtValue.Key);
								}


								break;
							case 12:
								this.txtToActivity.Text=activity2Com.Name;
								break;
						}
						break;
					}
				}

				this.cmbResult.Text=xmlTransition.Result;
			}
		}

		private void LoadControl()
		{
			cmbRexec.Items.Add (WorkflowProcesses.XMLTransition.NO_REEXEC);
			cmbRexec.Items.Add (WorkflowProcesses.XMLTransition.YES_REEXEC);
			
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.ALWAYS_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.DEFAULT_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.EQUAL_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.NOTEQUAL_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.LESS_THAN_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.LESS_EQUAL_THAN_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.MORE_EQUAL_THAN_OPERATOR);
			cmbOperator.Items.Add (WorkflowProcesses.XMLTransition.MORE_THAN_OPERATOR);
		
			cmbResult.Items.Add(WorkflowProcesses.XMLTransition.DEFAULT_RESULT);
		}
	}
}
